package com.wzdigit.wms.raw.task;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.wms.raw.constant.SyncTypeConstant;
import com.wzdigit.wms.raw.domain.dto.IfWearepartHeaderDto;
import com.wzdigit.wms.raw.exception.SyncException;
import com.wzdigit.wms.raw.service.mid.IfWearepartHeaderService;
import com.wzdigit.wms.raw.service.mid.SyncErrorDataService;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/*易损件领退料同步到业务表
    * @author shuaic
    * @date 2021/7/26 16:01
*/
@ConditionalOnExpression("${schedule.common.switch}")
@Component
@Slf4j
public class IfWearepartToGdnOrAsnTask {

    @Autowired
    private IfWearepartHeaderService ifWearepartHeaderService;

    @Autowired
    private SyncErrorDataService syncErrorDataService;

    final String syncType = SyncTypeConstant.IF_WEAREPART_TO_GDN_OR_ASN;

    @Scheduled(cron = "0 0/5 * * * ?")
    public synchronized void syncData() {
        try {
            log.info("if:Wearepart => gdn or asn start...");
            long start = System.currentTimeMillis();
            List<IfWearepartHeaderDto> datas = ifWearepartHeaderService.selectSyncData();
            if (!CollectionUtil.isEmpty(datas)) {
                syncData(datas, false);
            }
            log.info("------if:Wearepart =>gdn or  asn end:{}------", System.currentTimeMillis() - start);
        } catch (RestStatusException ex) {
            ex.printStackTrace();
        }
    }

    public void syncData(List<IfWearepartHeaderDto> datas, boolean isSyncError) {
        for (IfWearepartHeaderDto data : datas) {
            if("1".equals(data.getAttribute1())){   //Y代表易损件领料单 同步到领料出库GDN
                try {
                    ifWearepartHeaderService.syncDataGdn(data, isSyncError);
                } catch (Exception e) {
                    exceptionHander(data, e, "易损件生产领料单同步失败，单号{}", "生产领料单同步失败：", "【生产领料单同步失败】");
                }
            }else if("2".equals(data.getAttribute1())){//B代表易损件退料单 退料到本厂 同步到ASN

                try {
                    ifWearepartHeaderService.syncDataAsn(data, isSyncError);
                } catch (Exception e) {
                    exceptionHander(data, e, "易损件生产退料单同步失败，单号{}", "生产退料单同步失败：", "【生产退料单同步失败】");
                }

            }

        }
    }

    public void exceptionHander(IfWearepartHeaderDto data, Exception e, String s, String s2, String s3) {
        log.warn(s, data.getZpckno());
        log.warn(s2 + e.getMessage() == null ? e.toString() : e.getMessage());
        String message = "";
        if (SyncException.class.isInstance(e)) {
            SyncException ex = (SyncException) e;
            message = ex.getFailedReason();
            if (message.length() >= 1000) {
                message = message.substring(0, 500);
            }
            log.info(s3 + ex.getFailedReason());
            syncErrorDataService.saveSyncErrorData(ex.getSyncErrorType(), ex.getCode(), message);
        } else {
            e.printStackTrace();
            if (e.getMessage() == null) {
                message = e.getClass().getName();
            } else {
                message = e.getMessage();
            }
            if (message.length() >= 1000) {
                message = message.substring(0, 500);
            }
            syncErrorDataService.saveSyncErrorData(syncType, data.getZpckno(), message);
        }
        e.printStackTrace();
    }
}
